package com.net.xpay.admin.controller.admin.rbac;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.net.common.exception.BusinessException;
import com.net.common.model.ResultData;
import com.net.common.model.ResultList;
import com.net.common.model.ResultPagedList;
import com.net.xpay.admin.po.AdminUserRolePo;
import com.net.xpay.common.domain.admin.rbac.Role;
import com.net.xpay.common.manager.admin.rbac.RoleManager;
import com.net.xpay.common.model.admin.rbac.search.RoleSearchModel;
import com.net.xpay.common.service.admin.rbac.RoleService;
import com.net.xpay.core.constant.PoseidonErrorCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 2019/11/13 14:50

 */
@Slf4j
@RestController
@RequestMapping("/admin/role")
@Api(tags = "运营/角色管理", description = "运营 角色管理 net")
public class AdminRoleController {

    @Autowired
    private RoleManager roleManager;

    @Autowired
    private RoleService roleService;

    /**
     * 角色列表
     */
    @ApiOperation("角色列表")
    @PostMapping("/search")
    @PreAuthorize("hasAnyRole('ADMIN', 'role::search')")
    public ResultPagedList<Role> search(@RequestBody RoleSearchModel searchModel) {
        List<Role> roleList = roleManager.search(searchModel);
        long count = roleManager.searchCount(searchModel);
        return new ResultPagedList<>(roleList, count, searchModel);
    }

    /**
     * 创建角色
     */
    @ApiOperation("创建角色")
    @PostMapping
    @PreAuthorize("hasAnyRole('ADMIN', 'role::add')")
    public ResultData<Role> createRole(@RequestBody Role role) {
        if (Strings.isNullOrEmpty(role.getName())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "角色不能为空");
        }
        Role selected = roleManager.findByName(role.getName());
        if (selected != null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "角色名重复");
        }
        role.setId(null);
        roleManager.save(role);
        return new ResultData<>(role);
    }

    /**
     * 所有角色
     */
    @ApiOperation("所有角色")
    @PostMapping("/list")
    @PreAuthorize("hasAnyRole('ADMIN', 'role::list')")
    public ResultList<Role> listRole() {
        List<Role> roles = roleManager.listAll();
        return new ResultList<>(roles);
    }

    /**
     * 保存用户角色
     */
    @ApiOperation("保存用户角色")
    @PostMapping("/save-roles")
    @PreAuthorize("hasAnyRole('ADMIN', 'role:save-roles')")
    public ResultData<Boolean> saveAdminUserRoles(@RequestBody AdminUserRolePo po) {
        Preconditions.checkArgument(po != null && po.getAdminUserId() != null);
        if (CollectionUtils.isEmpty(po.getRoleIds())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "至少选择一个角色");
        }
        roleService.saveAdminUserRoles(po.getAdminUserId(), po.getRoleIds());
        return new ResultData<>(true);
    }
}
